package durak.client.view;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import durak.client.control.ICardListener;
import durak.common.model.Card;

public class CardLabel extends JLabel {

	int rotateAngle = 0;
	ImageIcon image;
	
	public CardLabel(final Card card, final ICardListener cardListener) {
		super();
		init(card, cardListener, false);
	}


	/**
	 * @param card
	 * @param cardListener
	 */
	private void init(final Card card, final ICardListener cardListener, boolean rotate) {
		try {
			if(rotate) {
				image = ImageUtils.getImageIconScaledRotated(card.toFileName()+".png", 80, 100, 90);
			} else {
				image = ImageUtils.getImageIconScaled(card.toFileName()+".png", 80, 100);
			}
			this.setIcon(image);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("Failed fetch image for card: " + card.toFileName());
		}
		this.setHorizontalAlignment(SwingConstants.CENTER);
		
		setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
		setVerticalTextPosition(SwingConstants.BOTTOM);
		//setHorizontalTextPosition(JLabel.CENTER);
		this.setVisible(true);
		setSize(image.getIconWidth(), image.getIconHeight());
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				cardListener.cardClickedAction(card);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
				repaint();
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.WHITE));
				repaint();
			}
		});
	}

	
	public CardLabel(final Card card, final ICardListener cardListener, int rotateAngle) {
		init(card, cardListener, true);
		this.rotateAngle = rotateAngle;
	}
	
	public CardLabel(String text) {
		super(text);
		// TODO Auto-generated constructor stub
	}

	public CardLabel(Icon image) {
		super(image);
		// TODO Auto-generated constructor stub
	}

	public CardLabel(Icon image, int horizontalAlignment) {
		super(image, horizontalAlignment);
		// TODO Auto-generated constructor stub
	}

	public CardLabel(String text, Icon icon, int horizontalAlignment) {
		super(text, icon, horizontalAlignment);
		// TODO Auto-generated constructor stub
	}


	public Image getImage() {
		// TODO Auto-generated method stub
		return image.getImage();
	}
}
